/* application.cc
* This file is part of ncursesxx library
* Copyright (c) 2003,2004 by Stanislav Ievlev
*
* This file is covered by the GNU Library General Public License,
* which should be included with libncursesxx as the file COPYING.
*/
#include <stdlib.h>
#include <stdexcept>

#include <term.h>

#include <ncurses/assert.hh>
#include <ncurses/application.hh>

using namespace ncurses;

application::application()
{
	//init ncurses enviroment
	//We use newterm/delscreen instead of initscr to reduce number of memory leaks
	const char *name;
	if (((name = getenv("TERM")) == NULL) || ('\0' == *name)) name = "unknown";
	screen_ = ::newterm(const_cast<char*>(name), stdout, stdin);

	if (!screen_) throw std::runtime_error("newterm failed");

	::def_prog_mode();
	NCURSES_ASSERT(::cbreak());
	NCURSES_ASSERT(::noecho());
	NCURSES_ASSERT(::nonl());
	NCURSES_ASSERT(::keypad(::stdscr, TRUE));

	if (::has_colors()) NCURSES_ASSERT(::start_color());

#ifdef NCURSES_MOUSE_VERSION
	::mousemask(ALL_MOUSE_EVENTS | REPORT_MOUSE_POSITION, static_cast<mmask_t*>(0));
#endif
	cursor_off(); //turn off cursor, by default
}

application::~application()
{
	::endwin();
	::delscreen(screen_);
}

int application::width() const
{
	return ::COLS;
}

int application::height() const
{
	return ::LINES;
}

void application::cursor_on()
{
	//first try to made cursor super visible, otherwise made it normal
	::curs_set((cursor_visible) ? 2 : 1);
}

void application::cursor_off()
{
	::curs_set(0); //turn on cursor
}
